﻿using System;

using System.Data;
using System.Drawing;
using System.Windows.Forms;
using ConfExpert.Forms.Base;
using DevExpress.XtraCharts;

namespace ConfExpert.Forms.Sistem
{
    public partial class WhosOnline : XForm
    {
        int tip;
        public WhosOnline(int _tip)
        {
            InitializeComponent();
            tip = _tip;
        }

        DataTable dt;
        BindingSource bs2;
        private void WhosOnline_Load(object sender, EventArgs e)
        {
            if (!ds.Tables.Contains("gunlukaktif"))
            {
                dt = new DataTable("gunlukaktif");
                dt.Columns.Add("gunluk", typeof(int));
                dt.Columns.Add("aktif", typeof(int));
                ds.Tables.Add(dt);
            }

            bs2 = new BindingSource(ds, "xOnline2");

            btnPrev.Visibility=btnNext.Visibility=dateEdit1.Visibility = ytk(62) ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;

            chartControl1.DataSource = bs2;
            chartControl1.RefreshDataOnRepaint = false;
            chartControl1.CacheToMemory = true;

            DateTime now = DateTime.Now;
            beyaz = new ConstantLine(now.ToShortTimeString(), now); //new DateTime(1900, 1, 1, now.Hour, now.Minute, 0).AddMinutes(2));
            beyaz.LineStyle.DashStyle = DevExpress.XtraCharts.DashStyle.DashDot;
            beyaz.LineStyle.Thickness = 2;
            beyaz.Color = Color.BlueViolet;
            ((DevExpress.XtraCharts.XYDiagram)(chartControl1.Diagram)).AxisY.ConstantLines.Add(beyaz);
            //((DevExpress.XtraCharts.XYDiagram)(chartControl1.Diagram)).EnableAxisXScrolling =
            //    ((DevExpress.XtraCharts.XYDiagram)(chartControl1.Diagram)).EnableAxisXZooming =
            //((DevExpress.XtraCharts.XYDiagram)(chartControl1.Diagram)).EnableAxisYScrolling =
            //    ((DevExpress.XtraCharts.XYDiagram)(chartControl1.Diagram)).EnableAxisYZooming = true;


            btnNext.ItemClick += btnNext_ItemClick;
            btnPrev.ItemClick += btnPrev_ItemClick;
            dateEdit1.EditValueChanged += new EventHandler(dateEdit1_EditValueChanged);
            dateEdit1.EditValue = now.Date;


            btnGuncelle.ItemClick+=btnGuncelle_ItemClick;



            //GetDetails("xOnline3", "GetUserStatistics", new DateTime(2010, 3, 1));
            timerD = new System.Timers.Timer(600000);    //update once a minute
            //timerD.SynchronizingObject = this;  // pivot;
            timerD.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
            timerD.AutoReset = true;
            timerD.Enabled = true;

            this.HandleDestroyed += new EventHandler(WhosOnline_HandleDestroyed);


        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MyRefresh();
        }

        void btnPrev_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            dateEdit1.EditValue = ((DateTime)dateEdit1.EditValue).AddDays(-1);
        }

        void btnNext_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            dateEdit1.EditValue = ((DateTime)dateEdit1.EditValue).AddDays(1);
        }
        static System.Timers.Timer timerD = null;

        DateTime gun;

        private void dateEdit1_EditValueChanged(object sender, EventArgs e)
        {
            gun = ((DateTime)dateEdit1.EditValue).Date;
            MyRefresh();
        }

        void WhosOnline_HandleDestroyed(object sender, EventArgs e)
        {
            isHandleDestroyed = true;
        }

        bool isHandleDestroyed = false;
        private delegate void UpdateDataSourceDelegate();
        private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            MyRefresh();
        }

        private void MyRefresh()
        {
            if (!isHandleDestroyed)
                this.Invoke(new UpdateDataSourceDelegate(RefreshData));
        }

        void RefreshData()
        {
            ds.xOnline2.Clear();
                ds.Tables["gunlukaktif"].Clear();
            GetDetails("xOnline2;gunlukaktif", "GetActiveUsers", gun, tip);

            txtInfo.Caption = "Aktif kullanıcı: " + ds.Tables["gunlukaktif"].Rows[0]["aktif"].ToString() + " (bugün: " + ds.Tables["gunlukaktif"].Rows[0]["gunluk"].ToString() + ")";

            //((DevExpress.XtraCharts.XYDiagram)chartControl2.Diagram).AxisX.Range.MaxValue = gun.AddDays(1);
            //((DevExpress.XtraCharts.XYDiagram)chartControl2.Diagram).AxisX.Range.MinValue = gun;

            if (gun >= (DateTime)((DevExpress.XtraCharts.XYDiagram)chartControl1.Diagram).AxisY.VisualRange.MaxValue)
            {
                ((DevExpress.XtraCharts.XYDiagram)chartControl1.Diagram).AxisY.VisualRange.MaxValue = gun.AddDays(1);
                ((DevExpress.XtraCharts.XYDiagram)chartControl1.Diagram).AxisY.VisualRange.MinValue = gun;
            }
            else
            {
                ((DevExpress.XtraCharts.XYDiagram)chartControl1.Diagram).AxisY.VisualRange.MinValue = gun;
                ((DevExpress.XtraCharts.XYDiagram)chartControl1.Diagram).AxisY.VisualRange.MaxValue = gun.AddDays(1);
            }

            //((DevExpress.XtraCharts.XYDiagram)chartControl1.Diagram).AxisY.Range.MinValue = gun;
            //((DevExpress.XtraCharts.XYDiagram)chartControl1.Diagram).AxisY.Range.MaxValue = gun.AddDays(1);
            ((PointSeriesView)chartControl1.SeriesTemplate.View).PointMarkerOptions.Size = 4;

            DrawConstantLine();
        }


        ConstantLine beyaz;
        
        private void DrawConstantLine()
        {
            DateTime now = DateTime.Now;    //"Current progress"
            beyaz.AxisValue = now;
            beyaz.Name = now.ToShortTimeString();

            //beyaz.AxisValue = new DateTime(1900, 1, 1, now.Hour, now.Minute, 0).AddMinutes(2);
        }

    }
}